<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Mod_pitpara extends MYAD_Controller {

    function __construct() {
        parent::__construct();
         $this->load->model('Mmod_pitpara','Pitpara');
    }
    
    /**
     * @author Thanh NV        
     * @function index      
     * @This function return PIT Parameter List  
     */
    function index(){   
        $this->load->library('pagination');             
        $total_data = $this->Pitpara->count_all(); 
	$perpage	= 10;
        $data['pagination'] = $this->paginationData($total_data,$perpage,'/pit-parameter');
        $offset = ($this->uri->segment(2)=='') ? 0 : $this->uri->segment(2);         
	$data['data'] = $this->Pitpara->listAllPagination($perpage, $offset,'ParameterId'); 
        $data['messsageError']=(count($data['data'])==0) ? $this->lang->line('CR_null') : "";
        $data['page']=$offset;
        $data['title']=$this->lang->line('ppl__Title');
        $data['show'] = "index";
        $this->load->view('layout',$data);
    }
    
    /**
     * @author Thanh NV        
     * @function addSalaryParameter 
     * @This function add PIT parameter
     */
	function addPitParameter(){              
        $action=$this->uri->segment(2);        
        /* check exists */
        $id= !empty($_POST['hidParameterID']) ? $_POST['hidParameterID'] : NULL;     
        if($action=='edit' && $id==NULL) redirect('/pit-parameter');        
        if (!is_null($id)) {
            
            $data=$this->Pitpara->listIdName('ParameterId',$id);
            
                if (!$data) {
                    redirect('/pit-parameter');
                }
        }
         
        if($this->input->post('btnButton')){ 
            $_POST['txtApplyDate'] = mb_convert_kana($_POST['txtApplyDate'], 'a');
            $_POST['txtEmployeeDeductionAmount'] = mb_convert_kana($_POST['txtEmployeeDeductionAmount'], 'a');
            $_POST['txtDeductionPerDependant'] = mb_convert_kana($_POST['txtDeductionPerDependant'], 'a');
                $this->load->library('form_validation');
                $this->form_validation->set_rules('txtApplyDate', $this->lang->line('pcpr__Applydate'), 'trim|required|callback_validate_month');
                $this->form_validation->set_rules('txtEmployeeDeductionAmount', $this->lang->line('pcpr__Employeedeductionamount'), 'required|integer|greater_than[0]'); 
                $this->form_validation->set_rules('txtDeductionPerDependant',$this->lang->line('pcpr__Deductionpersondependant'), 'required|integer|greater_than[0]'); 
                if($this->form_validation->run() == TRUE){  
                    if(!empty($_POST['hidParameterID'])){
                         $data=array(               
                        'CompanyCode'=>$this->session->userdata('CompanyCode'),
                        'Upd_ID'=>$this->session->userdata('UserId'),
                        'Upd_Date'=>date("Y-m-d h:i:s"),     
                        'Del_Flag'=>($this->input->post('chkDelete')=='on') ? 1 : 0,     
                        'EmployeeDeductionAmount'=>$this->input->post('txtEmployeeDeductionAmount'),
                        'DeductionPerDependant'=>$this->input->post('txtDeductionPerDependant'),                
                        'ApplyDate'=>str_replace("/","-",$this->input->post('txtApplyDate')).'-01',
                       );                         
                       }else{
                         $data=array(               
                        'CompanyCode'=>$this->session->userdata('CompanyCode'),
                        'Ins_ID'=>$this->session->userdata('UserId'),
                        'Ins_Date'=>date("Y-m-d h:i:s"),
                        'Upd_ID'=>$this->session->userdata('UserId'),
                        'Upd_Date'=>date("Y-m-d h:i:s"),     
                        'Del_Flag'=>($this->input->post('chkDelete')=='on') ? 1 : 0,     
                        'EmployeeDeductionAmount'=>$this->input->post('txtEmployeeDeductionAmount'),
                        'DeductionPerDependant'=>$this->input->post('txtDeductionPerDependant'),                
                        'ApplyDate'=>str_replace("/","-",$this->input->post('txtApplyDate')).'-01',
                       );  
                       }  
                       if(is_null($id)){  
                                 if($this->Pitpara->isApplyDate(str_replace("/","-",$this->input->post('txtApplyDate')).'-01')){
                                          $result =$this->Pitpara->insertData($data);
                                           //Check result   
                                          if($result){                       
                                             $data= array(
                                                      'flag' => TRUE,
                                                      'message' => $this->lang->line('emp_updateDB_success')
                                                  );
                                          }else{
                                             $data=array(
                                                          'flag' => FALSE,
                                                          'message' => $this->lang->line('emp_updateDB_fall')
                                                      );
                                              }
                               }else{
                                   $data=array(
                                                              'flag' => FALSE,
                                                              'message' => $this->lang->line('MC012')
                                                          );
                               } 
                         }else{
                               if($this->Pitpara->checkApplyDate(str_replace("/","-",$this->input->post('txtApplyDate')).'-01',$id)){
                                               $result =$this->Pitpara->updateDataName($data, $id, 'ParameterId');
                                              if($result){                       
                                                 $data= array(
                                                          'flag' => TRUE,
                                                          'message' => $this->lang->line('emp_updateDB_success')
                                                      );
                                              }else{
                                                 $data=array(
                                                              'flag' => FALSE,
                                                              'message' => $this->lang->line('emp_updateDB_fall')
                                                          );
                                                  }
                                  }else{
                                        $data=array(
                                                              'flag' => FALSE,
                                                              'message' => $this->lang->line('MC012')
                                                          );
                                     
                                  } 
                                  $data['Del_Flag']=($this->input->post('chkDelete')=='on') ? 1 : 0;
                          }
                          
                   
                }
//                $_POST['txtApplyDate'] = mb_convert_kana($_POST['txtApplyDate'], 'A');
//                $_POST['txtEmployeeDeductionAmount'] = mb_convert_kana($_POST['txtEmployeeDeductionAmount'], 'A');
//                $_POST['txtDeductionPerDependant'] = mb_convert_kana($_POST['txtDeductionPerDependant'], 'A');                
                $data['EmployeeDeductionAmount']=$this->input->post('txtEmployeeDeductionAmount');
                $data['DeductionPerDependant']=$this->input->post('txtDeductionPerDependant');
                $data['ApplyDate']=$this->input->post('txtApplyDate');
                $data['ParameterId']=$this->input->post('hidParameterID');
                   
        } else{
                $data=$this->Pitpara->listIdName('ParameterId',$id);
        }   
        
        if(isset($id) && !is_null($id)){
            $data['title']=$this->lang->line('pcpr__Title_update');
        }else{
            $data['title']=$this->lang->line('pcpr__Title');
        }
        $data['dialogTitle']=$this->lang->line('emp_dialog_title');
	$data['js'] = array('jquery-ui.js');
	$data['css'] = array('jquery-ui.css');		        
        $data['show'] = "add_pitParameter";
        $data['data'] = "";
        $this->load->view('layout',$data);		
	}
}
?>